package com.cangxuan.backend.controller;

import com.cangxuan.backend.annotation.Check;
import com.cangxuan.backend.annotation.check.Max;
import com.cangxuan.backend.annotation.check.NotNull;
import com.cangxuan.backend.bean.Page;
import com.cangxuan.backend.entity.Goods;
import com.cangxuan.backend.service.IGoodsService;
import io.swagger.annotations.*;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 藏品相关控制器
 */
@Api(tags = "藏品相关接口")
@RestController
@RequestMapping("goods")
public class GoodsController extends BaseController {

    IGoodsService goodsService;
    @Autowired
    public void setGoodsService(IGoodsService goodsService) {
        this.goodsService = goodsService;
    }

    @Data
    @ApiModel("藏品分页查询body")
    public static class GoodsPageBody{
        @ApiModelProperty("页码，从1开始")
        @NotNull(message = "请传递pageIndex")
        Integer pageIndex;
        @ApiModelProperty("页大小，就是每页查询的数量，最大为50条")
        @Max(value = 50, message = "pageSize不能大于50")
        Integer pageSize = 10;
        @ApiModelProperty("藏品名称")
        String name;
        @ApiModelProperty("藏品编号")
        String code;
    }

    /**
     * 分页获取藏品
     * @param goodsPageBody
     * @return
     */
    @PostMapping
    @ApiOperation("藏品分页查询")
    public Object page(@Check @RequestBody GoodsPageBody goodsPageBody) {
        Page<Goods> goodsPage = goodsService.page(goodsPageBody.getPageIndex(),
                goodsPageBody.getPageSize(),
                goodsPageBody.getName(),
                goodsPageBody.getCode());
        return success(goodsPage);
    }

    /**
     * 根据ID获取藏品
     * @param id
     * @return
     */
    @GetMapping("get/{id}")
    @ApiOperation("根据ID查询藏品")
    public Object get(@ApiParam("藏品ID") @PathVariable int id) {
        return success("请求成功", goodsService.get(id));
    }

}
